﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace znsd.Utility
{
    class SerializeHelper<T>
    {
        public String SerializerToJson(String path, List<T> datas)
        {
            try
            {
                JsonSerializer serializer = new JsonSerializer();
                using (var stream = new FileStream(path, FileMode.OpenOrCreate))
                {
                    StreamWriter write = new StreamWriter(stream);
                    write.AutoFlush = true;
                    serializer.Serialize(write, datas);

                }
                return "success";
            }
            catch (Exception e)
            {
                return "failure";
                throw new Exception("序列化异常" + e.Message);

            }

        }
        public List<T> DeserializerFromJson(String path, List<T> datas)
        {
            try
            {
                JsonSerializer serializer = new JsonSerializer();
                using (var stream = new FileStream(path, FileMode.OpenOrCreate))
                {
                    StreamReader reader = new StreamReader(stream);
                   
                   datas =  serializer.Deserialize<List<T>>(new JsonTextReader(reader));

                }
                return datas;
            }
            catch (Exception e)
            {
                return null;
                throw new Exception("序列化异常" + e.Message);

            }
        }

    }
}
